package controller;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.bean.ChuyenAn;
import model.dao.ChuyenAnDAO;

/**
 * Servlet implementation class ChuyenAnServlet
 */
@WebServlet("/ChuyenAnServlet")
public class ChuyenAnServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ChuyenAnServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		ChuyenAnDAO chuyenAnDAO = new ChuyenAnDAO();

		if (request.getParameter("tenchuyenan") != null
				&& request.getParameter("ghichu") != null) {

			String tenChuyenan = request.getParameter("tenchuyenan");
			String ghichu = request.getParameter("ghichu");
			int id = chuyenAnDAO.getChuyenanID();
			
			boolean them = chuyenAnDAO.themChuyenAn(id, tenChuyenan, ghichu);
			
			request.setAttribute("them", them ? "true" :"false");
			
			RequestDispatcher rd = request
					.getRequestDispatcher("form_chuyenan.jsp");
			rd.forward(request, response);

		}
		
		else if(request.getParameter("dsChuyenan") != null) {
			
			ArrayList<ChuyenAn> dsChuyenan = chuyenAnDAO.dsChuyenan();
			
			request.setAttribute("dsChuyenan", dsChuyenan);
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chuyenan.jsp");
			rd.forward(request, response);
		}
		
		else if (request.getParameter("them_ds") != null) {
			
			RequestDispatcher rd = request
					.getRequestDispatcher("form_chuyenan.jsp");
			rd.forward(request, response);
		} 
		
		else if(request.getParameter("xoa") != null) {
			
			String id = request.getParameter("xoa"); //request.getParameter("id");
			boolean xoa = chuyenAnDAO.xoaChuyenan(id);
			request.setAttribute("xoa",xoa ? "true":"false");
			
			ArrayList<ChuyenAn> dsChuyenan = chuyenAnDAO.dsChuyenan();	
			request.setAttribute("dsChuyenan", dsChuyenan);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chuyenan.jsp");
			rd.forward(request, response);
			
		}
		
		else if(request.getParameter("sua") != null) {
			
			String id = request.getParameter("id");
			
			ChuyenAn chuyenAn = chuyenAnDAO.getChuyenan(id);
			request.setAttribute("chuyenAn", chuyenAn);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("sua_chuyenan.jsp");
			rd.forward(request, response);
			
		}
		
		else if (request.getParameter("tenchuyenan2") != null) {
			
			String id = request.getParameter("idca");
			String tenca = request.getParameter("tenchuyenan2");
			String ghichu = request.getParameter("ghichu");
			
			boolean sua = chuyenAnDAO.capnhapCA(id, tenca, ghichu );
			request.setAttribute("sua", sua ? "true":"false");
			
			ChuyenAn chuyenAn = chuyenAnDAO.getChuyenan(id);
			request.setAttribute("chuyenAn", chuyenAn);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("sua_chuyenan.jsp");
			rd.forward(request, response);
		}
		else {
			ArrayList<ChuyenAn> dsChuyenan = chuyenAnDAO.dsChuyenan();	
			request.setAttribute("dsChuyenan", dsChuyenan);
			
			RequestDispatcher rd = request
					.getRequestDispatcher("ds_chuyenan.jsp");
			rd.forward(request, response);
		}
	}

}
